//
//  YZMChangeWithdrawPwdViewController.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/20.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMChangeWithdrawPwdViewController.h"
#import "YZMHintInputView.h"
#import "YZClient+Account.h"
#import "YZGeneralResultModel.h"

@interface YZMChangeWithdrawPwdViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) YZMHintInputView *oldPasswordView;
@property (nonatomic, strong) YZMHintInputView *passwordView;
@property (nonatomic, strong) YZMHintInputView *ConfirmPwdView;

@end

@implementation YZMChangeWithdrawPwdViewController {
    
    UIButton *_resetPwdButton;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    self.title = @"修改提现密码";
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    self.title = nil;
}

#pragma mark -- KeyboardNotification
- (void)showKeyboard:(NSNotification *)noti {
    NSDictionary *dic = noti.userInfo;
    //键盘弹出后的位置
    CGRect endFrame = [dic[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make){
        make.bottom.equalTo(self.view).offset(-endFrame.size.height);
    }];
}

- (void)hideKeyboard:(NSNotification *)noti {
    
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make){
        make.bottom.equalTo(self.view);
    }];
}

#pragma mark -- init

- (id)init {
    if (self = [super init]) {
        self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createObjects];
}

#pragma mark -- createObjects
- (void)createObjects {
    
    WS(ws)
    
    self.scrollView = [[UIScrollView alloc] init];
    CGSize contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 200);
    if (iPhone4 || iPhone5) {
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.contentSize = contentSize;
        self.scrollView.bounces = NO;
    }
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view);
    }];
    
    self.oldPasswordView = [[YZMHintInputView alloc] init];
    [self.scrollView addSubview:self.oldPasswordView];
    [self.oldPasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.scrollView.mas_top).with.offset(14);
        make.width.equalTo(ws.scrollView);
        make.left.equalTo(ws.scrollView.mas_left);
        make.height.mas_equalTo(InputViewHeight);
    }];
    self.oldPasswordView.inputLabel.text = @"原提现密码";
    self.oldPasswordView.textField.placeholder = @"请输入提现密码";
    self.oldPasswordView.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.oldPasswordView.textField.returnKeyType = UIReturnKeyNext;
    self.oldPasswordView.textField.secureTextEntry = YES;
    self.oldPasswordView.textField.delegate = self;
    
    self.oldPasswordView.lineTopView.hidden=NO;
    self.oldPasswordView.lineBottomView.hidden=NO;
    
    [self.oldPasswordView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    
    UIButton *getPasswordButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [getPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [getPasswordButton setTitleColor:[YZMAppStyle colorForIconColor] forState:UIControlStateNormal];
    [getPasswordButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [getPasswordButton addTarget:self action:@selector(resetPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.oldPasswordView addSubview:getPasswordButton];
    [self.oldPasswordView appendRightWidget:getPasswordButton isShowSegmentLine:YES];
    
    self.passwordView = [[YZMHintInputView alloc] init];
    [self.scrollView addSubview:self.passwordView];
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.oldPasswordView.mas_bottom).with.offset(ControlSpacing15);
        make.left.equalTo(ws.scrollView.mas_left);
        make.width.height.equalTo(ws.oldPasswordView);
    }];
    self.passwordView.inputLabel.text = @"提现密码";
    self.passwordView.textField.placeholder = @"请输入6位纯数字密码";
    self.passwordView.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordView.textField.returnKeyType = UIReturnKeyNext;
    self.passwordView.textField.secureTextEntry = YES;
    self.passwordView.textField.delegate = self;
    
    self.passwordView.lineTopView.hidden=NO;
    
    [self.passwordView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    
    self.ConfirmPwdView = [[YZMHintInputView alloc] init];
    [self.scrollView addSubview:self.ConfirmPwdView];
    [self.ConfirmPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.scrollView.mas_left);
        make.top.equalTo(ws.passwordView.mas_bottom);
        make.width.height.equalTo(ws.passwordView);
    }];
    
    self.ConfirmPwdView.inputLabel.text = @"确认密码";
    self.ConfirmPwdView.textField.placeholder = @"请再次确认提现密码";
    self.ConfirmPwdView.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.ConfirmPwdView.textField.returnKeyType = UIReturnKeyNext;
    self.ConfirmPwdView.textField.secureTextEntry = YES;
    
    self.ConfirmPwdView.lineBottomView.hidden=NO;
    
    self.ConfirmPwdView.textField.returnKeyType = UIReturnKeyDone;
    self.ConfirmPwdView.textField.delegate = self;
    [self.ConfirmPwdView.textField addTarget:self action:@selector(textFieldchange:) forControlEvents:UIControlEventEditingChanged];
    
    [self.oldPasswordView.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.ConfirmPwdView.textField.mas_left);
    }];
    [self.passwordView.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.ConfirmPwdView.textField.mas_left);
    }];
    
    self.passwordView.lineBottomView.hidden = YES;
    [self.ConfirmPwdView updateLineIndentationMethod:self.ConfirmPwdView.lineTopView enumeration:LINE_INDENT_SCENE_LEFT indentValue:ControlSpacing15];
    
    _resetPwdButton = [YZMAppStyle NewsubmitButton];
    [_resetPwdButton setTitle:@"确定" forState:UIControlStateNormal];
    [_resetPwdButton addTarget:self action:@selector(confirmButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    _resetPwdButton.enabled = NO;
    [self.scrollView addSubview:_resetPwdButton];
    [_resetPwdButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view).with.offset(ControlSpacing20);
        make.right.equalTo(ws.view).with.offset(-ControlSpacing20);
        make.top.equalTo(ws.ConfirmPwdView.mas_bottom).with.offset(ControlSpacing30);
    }];
    
}

#pragma mark -- buttonEvents

- (void)resetPassword {
    [self resignAllFirstResponder];
    
    [YZMHCMInstance pushViewControllerWithName:@"forgetWdPwd" andParams:nil];
}

- (void)confirmButtonPressed {
    [self resignAllFirstResponder];
    
    if (![self checkInputContent:self.oldPasswordView.textField] ||
        ![self checkInputContent:self.passwordView.textField] ||
        ![self checkInputContent:self.ConfirmPwdView.textField]) {
        return;
    }
    
    if (![self.passwordView.textField.text isEqualToString:self.ConfirmPwdView.textField.text]) {
        [MBProgressHUD showMessage: @"两次密码输入不一致"];
        return;
    }
    
    [YZClient changeWithdrawPwdWithOldPwd: self.oldPasswordView.textField.text andNewPwd: self.passwordView.textField.text  success:^(NSURLSessionDataTask *task,id responseObject) {
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            [MBProgressHUD showMessage: @"提现密码重置成功"];
            [YZMHCMInstance popViewController:nil animated:YES];
        } else
        {
            [topModel showMessage];
        }
    } failure:^(NSError *error) {
        [error yz_showMessage];
    }];
    
}

- (BOOL)checkInputContent:(UITextField *)textField {
    
    if (textField == self.oldPasswordView.textField || textField == self.passwordView.textField || textField == self.ConfirmPwdView.textField) {
        
        if ([self.passwordView.textField.text yz_isWithdrawPwdCode]) {
            return YES;
        } else {
            [self registerValidateFail:@"密码由6位纯数字组成"];
            return NO;
        }
    }
    
    return NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)resignAllFirstResponder {
    [self.oldPasswordView.textField resignFirstResponder];
    [self.passwordView.textField resignFirstResponder];
    [self.ConfirmPwdView.textField resignFirstResponder];
}

#pragma mark -- UITextFieldDelegate

//点击键盘return
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == self.oldPasswordView.textField) {
        
        [self.passwordView.textField becomeFirstResponder];
    } else if (textField == self.passwordView.textField) {
        
        [self.ConfirmPwdView.textField becomeFirstResponder];
    } else if (textField == self.ConfirmPwdView.textField) {
        
        [self.ConfirmPwdView.textField resignFirstResponder];
    }
    
    return YES;
}

- (void)textFieldchange:(UITextField *)send {
    
    if ([self.oldPasswordView.textField.text length] > 0 &&
        [self.passwordView.textField.text length] > 0 &&
        [self.ConfirmPwdView.textField.text length] > 0) {
        _resetPwdButton.enabled = YES;
        _resetPwdButton.layer.borderWidth=0.0;

        return;
    }
    
    _resetPwdButton.layer.borderWidth=1.0;
    _resetPwdButton.enabled = NO;
}

#pragma mark -- registerValid

- (void)registerValidateFail:(NSString *)message {
    
    if (message) {
        [MBProgressHUD showMessage:message];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
